package org.scmpatrol.scm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.scmpatrol.scm.action.SCMAction;

@Entity
@Table(name = "commit")
@NamedQuery(name = "max_revision", query = "select max(c.id) from SCMCommit c where c.repositoryId = :repositoryId")
public class SCMCommit implements Serializable {

	private static final long serialVersionUID = 2322539566762610641L;

	/**
	 * the scm patrol id
	 */
	@Id
	@GeneratedValue
	private Long id;

	/**
	 * The associated repository
	 */
	@Column(nullable = false, updatable = false)
	private Long repositoryId;

	/**
	 * SCM revision name (underlying scm revision id)
	 */
	@Column
	String scmRevision;

	/**
	 * the author of the revision
	 */
	String author;

	/**
	 * Date of the revision
	 */
	Date date;

	/**
	 * The message associated with this commit
	 */
	String message;

	/**
	 * List of action done in this revision
	 */
	@OneToMany(cascade = CascadeType.ALL)
	List<SCMAction> actions = new ArrayList<SCMAction>();

	public Long getId() {
		return id;
	}

	public String getScmRevision() {
		return scmRevision;
	}

	public void setScmRevision(String scmRevision) {
		this.scmRevision = scmRevision;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<SCMAction> getActions() {
		return Collections.unmodifiableList(actions);
	}

	public void addAction(SCMAction action) {
		this.actions.add(action);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public void setRepositoryId(Long repositoryId) {
		this.repositoryId = repositoryId;
	}

	public Long getRepositoryId() {
		return repositoryId;
	}
}
